package com.example.raspberry_pi_demob.controller;

import com.example.raspberry_pi_demob.entity.AllData;
import com.example.raspberry_pi_demob.entity.Equipment;
import com.example.raspberry_pi_demob.service.AllDataService;
import com.example.raspberry_pi_demob.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path="/allData")
public class AllDataController {
    //注入 alldata sevice
    @Autowired
    private AllDataService allDataService;
    @Autowired
    private EquipmentService equipmentService;
    Map<String,Integer> intervalMap = new HashMap<String,Integer>();

    /**
     * 查询所有信息列表
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<AllData> getAllDatas(){
        return allDataService.findAllDataList();
    }

    @RequestMapping(value = "/equipment",method = RequestMethod.GET)
    public List<Equipment> getAllEquipment(){
        return equipmentService.findEquipmentList();
    }

    @RequestMapping(value = "/getEquipment/{macAddress}",method = RequestMethod.GET)
    public Equipment getEquipment(@PathVariable("macAddress") String macAddress){
        return equipmentService.findEquipment(macAddress);
    }
    /**
     * 通过时间戳timestamp查询一个设备的信息
     * @return
     */
    @RequestMapping(value = "/getByTimestamp/{timestamp}",method = RequestMethod.GET)
    public  AllData getDataByTimestamp(@PathVariable("timestamp") Integer timestamp){
        return allDataService.findAllDataByTimestamp(timestamp);
    }

    /**
     * 通过macAddress查询设备的信息
     * @return
     */
    @RequestMapping(value = "/getByMacAddress/{macAddress}",method = RequestMethod.GET)
    public List<AllData> getDataByMacAddress(@PathVariable("macAddress") String macAddress){
        return allDataService.findAllDataByMacAddress(macAddress);
    }

    /**
     * 查询某一设备某个时间段的信息
     * @param startTime
     * @param endTime
     * @return
     */
    //根据书写的时间到service里进行比较
    @RequestMapping(value = "/getByTimeBucket",method = RequestMethod.GET)
    public  List<AllData> getDataByTime(@RequestParam(name ="startTime") String startTime,@RequestParam(name ="endTime")String endTime){
        return allDataService.findAllDataByTimeBucket(startTime,endTime);
    }

    /**
     * 删除一个信息
     * @param timestamp
     * @return
     */
    @RequestMapping(value = "/delete/{timestamp}",method = RequestMethod.DELETE)
    public  String delDataByTimeStamp(@PathVariable("timestamp") Integer timestamp){
        int flag = allDataService.delete(timestamp);
        return (flag == 1 ?  "delete success!" : "delete fail!");
    }

    /**
     * 修改时间间隔信息
     * @param macAddress
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public  String updateAllData(@RequestParam(value = "macAddress")String macAddress,
                                 @RequestParam(value = "updateInterval")Integer updateinterval){
        Integer tempInterval = intervalMap.get(macAddress);
        if(tempInterval != updateinterval)
        {
            intervalMap.remove(macAddress);
            intervalMap.put(macAddress,updateinterval);
        }
        String display = "macAddress:" + macAddress +"_updateInterval:_" + updateinterval.toString();
        return display;
    }

    /**
     * 新增信息
     * @param
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public  Integer postAllData(@RequestBody AllData allData){
        //插入表equipment
        Equipment tempEquipment = new Equipment();
        tempEquipment.setMacAddress(allData.getMacAddress());
        tempEquipment.setRaspberryPiName(allData.getRaspberryPiName());
        int result = equipmentService.save(tempEquipment);
        //插入alldata表并返回时间间隔
        int t= allDataService.save(allData);
        String insertMacAddress = allData.getMacAddress();
        Integer insertInterval = allData.getUpdateInterval();
        if(intervalMap.get(insertMacAddress) == null) {
            intervalMap.put(insertMacAddress, insertInterval);
        }
        Integer nowInterval = intervalMap.get(insertMacAddress);
        
        //检验两个表是否都插入成功
        int s = result + t;
        return (s==2 ? nowInterval : 0);
    }

}
